package com.itheima.sync.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.entity.PageResult;
import com.itheima.entity.Result;
import com.itheima.mapper.BrandMapper;
import com.itheima.pojo.Brand;
import com.itheima.sync.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service("BrandService")
public class BrandServiceImpl implements BrandService {
    @Autowired
    private BrandMapper brandMapper;

    //1.查询所有用户
    @Override
    public Result findAll() {
        List<Brand> data = this.brandMapper.findAll();
        return new Result<>(20000, true, "查询成功", data);
    }


    //2.添加用户
    @Override
    public Result add(Brand brand) {
        Integer data = this.brandMapper.add(brand);
        if (data > 0) {
            return new Result<>(200, true, "添加成功", brand);
        } else {
            return new Result<>(403, false, "添加失败", brand);
        }
    }

    //3.分页查询
    @Override
    public Result findPage(Integer page, Integer size) {
        PageHelper.startPage(page, size);
        List<Brand> list = this.brandMapper.findAll();
        PageInfo<Brand> pageInfo = new PageInfo<>(list);
        PageResult data = new PageResult(pageInfo.getTotal(), pageInfo.getList());
        return new Result<>(20000, true, "查询成功", data);
    }


    @Override
    public Result findById(Integer id) {
        Brand data = this.brandMapper.findById(id);
        return new Result<>(20000, true, "查询成功", data);
    }

    @Override
    public Result update(Integer id, Brand brand) {
        brand.setId(id);
        Integer data = this.brandMapper.update(brand);
        if (data == 1) {
            return new Result<>(20000, true, "更新成功", brand);
        } else {
            return new Result<>(403, false, "更新失败", brand);
        }
    }

    @Override
    public Result delete(Integer id) {
        Integer data = this.brandMapper.delete(id);
        if (data == 1) {
            return new Result<>(20000, true, "更新成功", id);
        } else {
            return new Result<>(403, false, "更新失败", id);
        }
    }
}

